import Vue from 'vue'
import Router from 'vue-router'
import RegLogin from '@/components/RegLogin/RegLogin'
import Home from '@/components/Home'
import Index from '@/components/Index'
import store from '../store/index'


// import Chat from '@/components/Chat'
// import Source from '@/components/Source'
// import Collection from '@/components/Collection'
// import HomeworkPost from '@/components/HomeworkPost'
// import HomeworkCheck from '@/components/HomeworkCheck'
// import HomeworkStatistic from '@/components/HomeworkStatistic'
// import CourseClass from '@/components/CourseClass'
// import Personal from '@/components/Personal'
// import Security from '@/components/Security'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      component: RegLogin,

    }, {
      path : '/index',
      component : Index,
      children : [
        {
          path: '',
          component: Home,
          meta: {
            requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
          },
        }, {
          path : 'home',
          component : Home
        },{
          path: 'chat',
          component : resolve => require(['@/components/Chat/Chat.vue'],resolve)
        }, {
          path: 'source',
          component : resolve => require(['@/components/Source.vue'],resolve)
        },{
          path: 'collection',
          component : resolve => require(['@/components/Collection.vue'],resolve)
        },{
          path: 'homework_post',
          component : resolve => require(['@/components/HomeworkPost.vue'],resolve)
        },{
          path: 'homework_check',
          component : resolve => require(['@/components/HomeworkCheck.vue'],resolve)
        },{
          path: 'homework_statistic',
          component : resolve => require(['@/components/HomeworkStatistic.vue'],resolve)
        },{
          path: 'course_class',
          component : resolve => require(['@/components/CourseClass.vue'],resolve)
        },{
          path: 'personal',
          component : resolve => require(['@/components/Personal.vue'],resolve)
        },{
          path: 'security',
          component : resolve => require(['@/components/Security.vue'],resolve)
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
    if (store.state.user) {  // 通过vuex state获取当前的token是否存在
      next();
    }
    else {
      next({
        path: '/'
      })
    }
  }
  else {
    next();
  }
})

export default router


